<?php
include_once("http.class2.php");

$publicpoststr=<<<TEXT
GET {surl} HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, * / *{Referer}
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; Foxy/1; QQDownload 702; GTB7.2; Foxy/1; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C)
Accept-Encoding: gzip, deflate
Host: {host}
cookies:{cookies}
Connection: Keep-Alive


TEXT;

class h2c{
var $user_agent= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
var $header= array('image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*');

var $getheader=1;
var $wpost="";

	function h2c($wpost){
		$this->wpost=$wpost;
	}

	function setheader($xx=1){
		$this->getheader=$xx;
	}


	function post(){
		$wpost=$this->wpost;
		
		preg_match_all("/^Host\:(\s*.*)$/Uimx",$wpost,$nn);
		$host=trim(chop($nn[1][0]));
		$site="http://".$host;
		preg_match_all("/^(POST|GET)\s+(.*)\s+HTTP/Umx",$wpost,$nn);
		$url=preg_replace("/\/+$/","",$site)."/".preg_replace("/^\/+/","",chop($nn[2][0]));
		preg_match_all("/^Referer\:(\s*.*)$/Uimx",$wpost,$nn);
		$Referer=trim(chop($nn[1][0]));
		preg_match_all("/\r\n\r\n(.*)$/Uis",$wpost,$nn);
		$Querystring=trim(chop($nn[1][0]));


		preg_match_all("/boundary=(.*)$/Uim",$wpost,$nn);
		$boundary=trim(chop($nn[1][0]));
		$filename=$url.(preg_match("/\?/",$url)?"&":"?").$Querystring;
		$wpost.="\r\n\r\n";


		if(preg_match_all("/^Cookie\:\s*(.*)$/Uimx",$wpost,$nn)){
			$cookie="";
			$jj=preg_split("/\;/",$nn[1][0]);
			for($i=0;$i<count($jj);$i++){
				$cookie.=trim(chop($jj[$i])).";";
			}
		}

		$ch = curl_init();
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
		curl_setopt($ch, CURLOPT_HEADER, $this->getheader);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//跳转
		if(preg_match("/multipart(?!\/)/Usx",$wpost)){
			curl_setopt($ch, CURLOPT_VERBOSE, 1);
			curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary")); 
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		}

		if($Referer!="") curl_setopt($ch, CURLOPT_REFERER, $Referer);
		if($cookie!="") curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $Querystring);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);	

		$result=curl_exec($ch);

		if(@gzinflate(substr($result ,10))){
		$result= gzinflate(substr($result ,10));
		}
	return $result;
	}
	
	
	function get(){
		$wpost=$this->wpost;
		preg_match_all("/^Host\:(\s*.*)$/Uimx",$wpost,$nn);
		$host=trim(chop($nn[1][0]));
		$site="http://".$host;
		preg_match_all("/^(POST|GET)\s+(.*)\s+HTTP/Umx",$wpost,$nn);
		$url=preg_replace("/\/+$/","",$site)."/".preg_replace("/^\/+/","",chop($nn[2][0]));
		preg_match_all("/^Referer\:(\s*.*)$/Uimx",$wpost,$nn);
		$Referer=trim(chop($nn[1][0]));
		preg_match_all("/\r\n\r\n(.*)$/Uis",$wpost,$nn);
		$Querystring=trim(chop($nn[1][0]));
		preg_match_all("/boundary=(.*)$/Uim",$wpost,$nn);
		$boundary=trim(chop($nn[1][0]));
		$filename=$url;
		//$filename=$url.(preg_match("/\?/",$url)?"&":"?").$Querystring;
		$user_agent="Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)";
	    $POSTFIELDS= $Querystring."login=$name&passwd=$password";
		//$wpost.="\r\n\r\n";

		if(preg_match_all("/^Cookie\:\s*(.*)$/Uimx",$wpost,$nn)){
			$cookie="";
			$jj=preg_split("/\;/",$nn[1][0]);
			for($i=0;$i<count($jj);$i++){
				$cookie.=trim(chop($jj[$i])).";";
			}
		}

		$timeout = 60;
		$ch = curl_init();
		
		curl_setopt ($ch, CURLOPT_URL, $filename);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		//curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)");
		if($nn[1][0]=="POST") curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_HEADER, $this->header);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//跳转
		if(preg_match("/multipart/Uisx",$wpost)){
			curl_setopt($ch, CURLOPT_VERBOSE, 1);
			curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary")); 
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		}
		curl_setopt($ch, CURLOPT_HEADER, $this->getheader);//要求输出头信息
		if($Referer!="") curl_setopt($ch, CURLOPT_REFERER, $Referer);
		if($cookie!="") curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		if($Querystring!=""&&$nn[1][0]=="POST"){
			curl_setopt($ch, CURLOPT_POSTFIELDS, $Querystring);
		}
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	
		$result=curl_exec($ch);
		
		if(@gzinflate(substr($result ,10))){
		$result= gzinflate(substr($result ,10));
		}

		return $result;
	}
}


function curl($post,$cookie="",$haveReferer=1){
$POSTFIELDS="";
    if(substr($post,0,7)=="http://"){
        $post=urltopoststr($post,$cookie,$haveReferer);
    }
	//echo $post;exit;

	preg_match_all("/^Host\:(\s*.*)$/Uimx",$post,$nn);
	$host=trim(chop($nn[1][0]));
	$site="http://".$host;
	preg_match_all("/^(POST|GET)\s+(.*)\s+HTTP/Umx",$post,$uu);
	$url=preg_replace("/\/+$/","",$site)."/".preg_replace("/^\/+/","",chop($uu[2][0]));
	preg_match_all("/^Referer\:(\s*.*)$/Uimx",$post,$nn);
	$Referer=trim(chop($nn[1][0]));
	preg_match_all("/(?:\r\n\r\n|\n\n)(.*)$/Uis",$post,$nn);
	$Querystring=trim(chop($nn[1][0]));
	preg_match_all("/boundary=(.*)$/Uim",$post,$nn);
	$boundary=trim(chop($nn[1][0]));
    $filename=$url;
	//$filename=$url.(preg_match("/\?/",$url)?"&":"?").$Querystring;
    //$filename=preg_replace("/http\:\/\/[^\/]*\//Uisx","/",$filename);
	$user_agent="Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)";
	$POSTFIELDS= $Querystring;

	$ch = curl_init();
    
    	
	$timeout = 60;
	curl_setopt ($ch, CURLOPT_URL, $filename);
	//curl_setopt ($ch, CURLOPT_MUTE, 0);
	curl_setopt ($ch, CURLOPT_VERBOSE, 0);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)");
	curl_setopt($ch, CURLOPT_HEADER, 1);//要求输出头信息
	if($uu[1][0]=="POST") curl_setopt($ch, CURLOPT_POST, 1);
	if(preg_match("/multipart/Uisx",$post)){
		curl_setopt($ch, CURLOPT_VERBOSE, 1);
		curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary")); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
	}
	if($Referer!="") curl_setopt($ch, CURLOPT_REFERER, $Referer);
	if($cookie!="") curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	if($Querystring!=""&&$uu[1][0]=="POST"){
		curl_setopt($ch, CURLOPT_POSTFIELDS, $Querystring);
	}
	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	$result = curl_exec($ch);
	curl_close($ch);

    preg_match("/(^.*)(?:\r\n\r\n|\n\n)(.*?)$/Uis",$result,$nn);
    $header=$nn[1];
    $body=$nn[2];
    
    if(@gzinflate(substr($body ,10))){
        $body= @gzinflate(substr($body ,10));
    }

	
	//---------------解chunk编码

	if(strpos($result,'chunk')){
	    $tmpfile = tempnam('/tmp','webcache'); 
	    $fp = fopen($tmpfile,'w'); 
	    fwrite($fp,$result); 
	    fclose($fp); 
	    ob_start(); 
	    readgzfile($tmpfile); 
	    $result = ob_get_contents(); 
	    ob_end_clean(); 
	    unlink($tmpfile);
    }
    
    $result=$header."\r\n\r\n".$body;
	$nr=$result;
	//$nr=preg_replace("/<(\/?script[^>]*)>/Uisx","<!--\\1-->",$nr);
	return $nr;
}

function curl2($post,$cookie="",$haveReferer=1){
$POSTFIELDS="";
    if(substr($post,0,7)=="http://"){
        $post=urltopoststr($post,$cookie,$haveReferer);
    }
	preg_match_all("/^Host\:(\s*.*)$/Uimx",$post,$nn);
	$host=trim(chop($nn[1][0]));
	$site="http://".$host;
	preg_match_all("/^(POST|GET)\s+(.*)\s+HTTP/Umx",$post,$uu);
	$url=preg_replace("/\/+$/","",$site)."/".preg_replace("/^\/+/","",chop($uu[2][0]));
	preg_match_all("/^Referer\:(\s*.*)$/Uimx",$post,$nn);
	$Referer=trim(chop($nn[1][0]));
	preg_match_all("/\r\n\r\n(.*)$/Uis",$post,$nn);
	$Querystring=trim(chop($nn[1][0]));
	preg_match_all("/boundary=(.*)$/Uim",$post,$nn);
	$boundary=trim(chop($nn[1][0]));
	$filename=$url;
	//$filename=$url.(preg_match("/\?/",$url)?"&":"?").$Querystring;
	$user_agent="Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)";
	$POSTFIELDS= $Querystring;

	$ch = curl_init();
	
	$timeout = 60;
	curl_setopt ($ch, CURLOPT_URL, $filename);
	//curl_setopt ($ch, CURLOPT_MUTE, 0);
	curl_setopt ($ch, CURLOPT_VERBOSE, 0);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/99.99 (compatible; MSIE 99.99; Windows XP 99.99)");
	curl_setopt($ch, CURLOPT_HEADER, 0);//要求输出头信息
	if($uu[1][0]=="POST") curl_setopt($ch, CURLOPT_POST, 1);
	if(preg_match("/multipart/Uisx",$post)){
		curl_setopt($ch, CURLOPT_VERBOSE, 1);
		curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary")); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
	}
	if($Referer!="") curl_setopt($ch, CURLOPT_REFERER, $Referer);
	if($cookie!="") curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	if($Querystring!=""&&$uu[1][0]=="POST"){
		curl_setopt($ch, CURLOPT_POSTFIELDS, $Querystring);
	}

	curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	
	$result = @curl_exec($ch);
	curl_close($ch);

	preg_match_all("/(^.*)\r\n\r\n(.*?)$/Uis",$result,$nn);

	if(@gzinflate(substr($body ,10))){
		$result= @gzinflate(substr($nn[2][0] ,10));
	}
	//$result=$body;
	$nr=$result;
	//$cookie=getcookie($result);
	//setcookieto();
	//$nr=preg_replace("/<(\/?script[^>]*)>/Uisx","<!--\\1-->",$nr);
	return $nr;
}


function zq($url,$ywpost=""){
global $isuft8;


$nr="";
if(trim($ywpost)!=""){
    $ywpost=preg_replace("/(GET\s+)([^\r\n\s]+)(\s+HTTP)/isx","\\1".preg_replace("/http\:\/\/[^\/]+/isx","",$url)."\\3",$ywpost);
	$ch=new h2c($ywpost);
	$nr=$ch->get();	
	//$fn=fopen("c:/1.txt","w");fwrite($fn,$ywpost."\r\n\r\n".$nr);fclose($fn);
}



if(strlen($nr)==0){
//echo "\r\n正在偿试第1种方法(file_get_contents)抓取".$url."\r\n";
$nr=@file_get_contents($url);
}


if(strlen($nr)==2){

preg_match_all("/http\:\/\/([^\/]*)\/?(.*)$/isx",$url,$hosts);
$host=$hosts[1][0];
$wpost=<<<TEXT
GET /{$hosts[2][0]} HTTP/1.1
Accept: */*
Referer: http://{$host}/{$hosts[2][0]}
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)
Host: $host
Connection: Keep-Alive


TEXT;


	//echo "\r\n正在偿试第1种方法(CHTTP)抓取".$url."\r\n";
	preg_match_all("/^Host\:(\s*.*)$/Uimx",$wpost,$nn);
	$host=trim(chop($nn[1][0]));
	$site="http://".$host;
	preg_match_all("/^(POST|GET)\s+(.*)\s+HTTP/Umx",$wpost,$nn);
	$urls=preg_replace("/\/+$/","",$site)."/".preg_replace("/^\/+/","",chop($nn[2][0]));
	preg_match_all("/^Referer\:(\s*.*)$/Uimx",$wpost,$nn);
	$Referer=trim(chop($nn[1][0]));
	preg_match_all("/^([a-zA-Z0-9]*\=.*)$/imx",$wpost,$nn);
	$Querystring=trim(chop($nn[1][0]));
	$filename=$urls.(preg_match("/\?/",$urls)?"&":"?").$Querystring;

	$thisHttp = new cHTTP();
	$thisHttp->sethttpHeader($wpost);
	$thisHttp->getPage($urls);
	$nr = $thisHttp->getContent();
	if(@gzinflate(substr($msg ,10))){
		$nr= @gzinflate(substr($msg ,10));
	}
}

if(strlen($nr)==0){
//echo "\r\n正在偿试第2种方法(curl)抓取;".$url."\r\n";
preg_match_all("/http\:\/\/([^\/]*)\/?(.*)$/isx",$url,$hosts);
$host=$hosts[1][0];
$wpost=<<<TEXT
GET /{$hosts[2][0]} HTTP/1.1
Accept: */*
Referer: http://{$host}/{$hosts[2][0]}
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)
Host: $host
Connection: Keep-Alive


TEXT;
$ch=new h2c($wpost);
$nr=$ch->get();	
}


if(strlen($nr)==0){
preg_match_all("/http\:\/\/([^\/]*)\/?(.*)$/isx",$url,$hosts);
$host=trim($hosts[1][0]);
//echo "\r\n正在偿试第6种方法(fsockopen)抓取".$url."\r\n";
//echo $nr;

$wpost=<<<TEXT
GET /{$hosts[2][0]} HTTP/1.1
Accept: */*
Referer: http://{$host}/{$hosts[2][0]}
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)
Host: $host
Connection: Keep-Alive


TEXT;

		$fp=@fsockopen($host,80,&$errno, &$errstr,3);
		if(!$fp) {
				echo "$errstr ($errno)<br>\n";
		} else {
				fputs($fp,$fpost);
				$wnr="";
				while(!feof($fp)){
						stream_set_timeout($fp,   3); 
						$wnr.=fgets($fp,128);
						$info   =   stream_get_meta_data($fp); 
						if($info['timed_out']){
							$wnr="";
							break;
						}
				}
				$nr=stripslashes($wnr);
				fclose($fp);
		}

}

if(strlen($nr)==0){
//echo "\r\n正在偿试第3种方法(fread)抓取".$url."\r\n";
$fn=@fopen($url,"r");
$nr=@fread($fn,2048000);
@fclose($fn);
}

if(strlen($nr)==0){
//echo "\r\n正在偿试第4种方法(include)抓取".$url."\r\n";
ob_start();   
@include($url);   
$nr=ob_get_contents();   
ob_end_clean();
}



if(strlen($nr)==0){
//echo "\r\n正在偿试第5种方法(readfile)抓取".$url."\r\n";
$nr=@readfile($url,"r");
}  


	if(strlen($nr)==0)
	//echo "\r\n\r\n----------------------------------\r\n\r\n抓取失败,得空内容回\r\n\r\n----------------------------------\r\n\r\n";
    if($isuft8) $nr=iconv("UTF-8","GB2312//IGNORE",$nr);
    return $nr;
}

function urltopoststr($post,$cookies,$haveReferer=1){
    global $publicpoststr;
        preg_match_all("/(http\:\/\/([^\/]*?))(.*)$/Uimx",$wurl,$mm);
        $host=$mm[2][0];
        $surl=$mm[3][0];
        $Referer="\r\n$Referer: http://{$host}/";
        $publicpoststr=str_replace("{host}",$host,$publicpoststr);
		$publicpoststr=str_replace("{Referer}",$Referer,$publicpoststr);
        $publicpoststr=str_replace("{surl}",$surl,$publicpoststr);
        $publicpoststr=str_replace("{cookies}",$cookies,$publicpoststr);
        return trim($publicpoststr)."\r\n\r\n";
}

?>
